<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Bilibili 刷新Cookie</title>
</head>

<body>
  <div id="show">正在加载 wasm，生成Hash</div>
  <script src="/static/js/jquery.js"></script>
  <script src="//s1.hdslb.com/bfs/static/jinkela/long/wasm/wasm_ras_umd.js"></script>
  <script>
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
      return (false);
    }
    function kl(e) {
      let t = "";
      for (let n = 0; n < e.length; n++) t += "" + e.charCodeAt(n).toString(16);
      return t
    }
    function closeWindow() {
      window.location.href = "about:blank";
      window.close();
    }
    var timestamp = getQueryVariable("timestamp");
    wasmInit.default().then(() => {
      document.getElementById("show").innerHTML = "wasm 加载完毕";
      var t = timestamp ? timestamp : new Date().getTime() - 3000;
      var data = kl(`refresh_${t}`);
      console.log(`refresh_${t}`);
      var sufix = wasmInit.encrypt({
        data: data,
        digest: "SHA256"
      });
      var url = "https://www.bilibili.com/correspond/1/" + sufix;
      console.log(url);
      $.post("/cookieRefresh/refresh_csrf", {
        sufix: sufix,
      },
        function (data, status) {
          console.log(JSON.stringify(data));
          var count = 5;
          var timer = setInterval(function () {
            document.getElementById("show").innerHTML = "请求发送完毕，" + count + "s后关闭";
            if (count == 0) {
              clearInterval(timer);
              closeWindow();
            }
            count--;
          },
            1000);
        });
    });
  </script>
</body>

</html>